package com.zhkc.iess.auth.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.zhkc.iess.auth.api.CaptchaApi;
import com.zhkc.iess.auth.constant.RedisKeyConstant;
import com.zhkc.wisdom.common.mybatis.generator.IdGenerator;
import com.zhkc.wisdom.common.redis.agent.RedisAgent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class CaptchaController implements CaptchaApi {


    @Resource
    private RedisAgent redisAgent;

    @Override
    public Map<String, Object> captchaImage() {
        // 利用 hutool 工具，生成验证码图片资源
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        circleCaptcha.setBackground(Color.gray);

        long uuid = IdGenerator.generateId();
        //利用redis存储当前验证码的uuid,过期时间120秒
        redisAgent.setString(RedisKeyConstant.KAPTCHA_SESSION_KEY + uuid, circleCaptcha.getCode(), 120);

        //返回给前端的数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("uuid", uuid);
        dataMap.put("imgData", circleCaptcha.getImageBase64());
        return dataMap;
    }

}
